package com.baosight.sczx.zm.cd.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.domain.ZMCDDocuments; 
/**
 * 
 * <p>Title: ServicetZMCDDocuments.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2016-12-2 上午11:29:09  张志芳  Create
 *</pre>
 */
public class ServiceZMCD02 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:29:41 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
        String fdMiningId=UserSession.getInSessionProperty("miningCode").toString();
        if(fdMiningId.equals("C.10223")){//煤质中心code D.10223
        	fdMiningId=InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
        }
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,fdMiningId));
		inInfo.addBlock(StaticBlocks.getBlockData("cmes.cd.doctype", new String[] { "地质地测", "生产设计", "通风设计", "机电设计", "质量标准化", "通知公告", "日常工作", "其它" },
				new String[] { "地质地测", "生产设计", "通风设计", "机电设计", "质量标准化", "通知公告", "日常工作", "其它" }, true));// 文档类型
		inInfo.addBlock(
				StaticBlocks.getBlockData("cmes.cd.businesstype", new String[] { "采煤", "掘进", "抽采", "机电", "运输", "通风", "安全", "其它" }, new String[] { "采煤", "掘进", "抽采", "机电", "运输", "通风", "安全", "其它" }, true));// 文档类型
		inInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.status", new String[]{"0", "1"}, new String[]{"草拟", "生效"}, true));//状态
		
		inInfo.set("fdCreatorId", UserSession.getInSessionProperty("UserCname"));// 登录人的默认值
		inInfo.set("fdDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		
		inInfo.addBlock(User.getInstance().getDepartmentPeople("cmes.cs.partpeople", "Code", "Name",
				(String)UserSession.getInSessionProperty("orgCode"), true));//人员下拉框
		
		return inInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:30:01 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		// String fdDirId = inInfo.getString("fdDirId");
        String fdMiningId=UserSession.getInSessionProperty("miningCode").toString();
        if(fdMiningId.equals("C.10223")){//煤质中心code D.10223
        	fdMiningId=InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
        }
		StaticTools.setQueryParameters(inInfo, "create", "1", true);// 添加查询条件
		EiInfo outInfo = super.query(inInfo, "tZMCDDocuments.query", new ZMCDDocuments());
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false, fdMiningId));
		outInfo.addBlock(User.getInstance().getDepartmentPeople("cmes.cs.partpeople", "Code", "Name", (String) UserSession.getInSessionProperty("orgCode"), true));// 人员下拉框
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cd.doctype", new String[] { "地质地测", "生产设计", "通风设计", "机电设计", "质量标准化", "通知公告", "日常工作", "其它" },
				new String[] { "地质地测", "生产设计", "通风设计", "机电设计", "质量标准化", "通知公告", "日常工作", "其它" }, true));// 文档类型
		outInfo.addBlock(
				StaticBlocks.getBlockData("cmes.cd.businesstype", new String[] { "采煤", "掘进", "抽采", "机电", "运输", "通风", "安全", "其它" }, new String[] { "采煤", "掘进", "抽采", "机电", "运输", "通风", "安全", "其它" }, true));// 文档类型
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.status", new String[]{"0", "1"}, new String[]{"草拟", "生效"}, true));//状态
		return outInfo;
	}
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:28:18 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo,"tZMCDDocuments.deleteFalse");
//		for(int i=0; i<inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
//		{
//			Documents.filesDelete("fdForeignId", inInfo.getBlock(EiConstant.resultBlock).getCellStr(i, "fdKeyId"));
//		}
//		
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}

	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:29:45 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
        String fdMiningId=UserSession.getInSessionProperty("miningCode").toString();
        if(fdMiningId.equals("C.10223")){//煤质中心code D.10223
        	fdMiningId=InitialData.getInstance().getEiBlockByNo("cmes.cn.meizhiCode").getCellStr(0, "display");
        }
		String fdDirId = inInfo.getString("fdDirId");
		inInfo.set("fdDirId", fdDirId);
		Object orgid = UserSession.getInSessionProperty("orgCode");
		if(orgid==null || "".equals(orgid))
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("必须使用业务用户，不能使用系统用户操作文档记录！");
			return inInfo;
		} 
		EiInfo out=new EiInfo(); 
		ZMCDDocuments _dom = new ZMCDDocuments();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			_dom.setFdDirId(fdDirId); 
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CD_DOCUMENT"));
			_dom.setFdMiningId(fdMiningId);
//			_dom.setFdCreatorId(UserSession.getUserId().toString());
			_dom.setFdDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
			//如果有附件上传功能的模块，需要增加以下生成docid方法。生成规则调用统一的方法。修改时不用管docid，也就是docid一旦生成不能修改。除非删除
			_dom.setFdDocId(StaticTools.getDocId());
			this.dao.insert("tZMCDDocuments.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
//		Documents.filesUpdate(inInfo, tablepk);  
	}
	

	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:30:16 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) { 
		EiInfo out=new EiInfo(); 
		ZMCDDocuments _dom = new ZMCDDocuments();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));  
			this.dao.update("tZMCDDocuments.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
//		Documents.filesUpdate(inInfo, tablepk);
	}
	
	/**
	 * 迁移
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServicetZMCDDocuments</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-2上午11:30:16 张志芳
	 * </p>
	 *
	 */
	public EiInfo move(EiInfo inInfo) {
		EiInfo outinfo = super.update(inInfo,"tZMCDDocuments.move");
		return outinfo;
	}
	
	/**
	 * 发布
	 * <p>Function: release</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-12 10:28:51 李红星
	 * </p>
	 *
	 */
	public EiInfo release(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo,"tZMCDDocuments.release");
        EiInfo outInfo = query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 工程
	 * <p>Function: getDepartment</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-15下午01:26:12 张志芳
	 * </p>
	 *
	 
	@SuppressWarnings("rawtypes")
	private EiBlock getEngineering()
	{
		EiBlock block = new EiBlock("cmes.cd.engineering");
		
		EiColumn eiColumn = new EiColumn("fdKeyId");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("fdName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = Engineering.getEngineerings(null);
		
		block.setRows(list);
		return block;
	}*/
	
	/**
	 * 校验标题重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-12-17下午02:48:30 张志芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unused" })
	private boolean validate(String name, String id) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdTitle", name);
		params.put("fdKeyId", id);
		
		List<?> result = dao.query("tZMCDDocuments.validate", params);
		if(result!=null && result.size() > 0) {
			return false;
		}
		return true;
	}
	
	/**
	 * 在CustomView中获得工程信息
	 * <p>Function: getEngineeringCustomView</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-4下午05:26:35 张志芳
	 * </p>
	 *
	 
	public EiInfo getEngineeringCustomView(EiInfo inInfo)
	{
		return Engineering.getEngineeringInfo(inInfo);
	}
	*/
}
